package com.service.product.impl;

import java.util.List;

import com.dao.ProductCategoryDao;
import com.entity.Cart;
import com.entity.Collect;
import com.entity.Product;
import com.entity.ProductCategory;
import com.service.product.ProductService;
import com.utils.CartUtil;
import com.utils.EmptyUtils;
import com.utils.MybatisUtil;
import com.utils.Pager;

/**
 * 商品接口实现类
 * @author 
 *
 */
public class ProductServiceImpl implements ProductService{

	
	@Override
	/**
	 * 根据商品分类等级查询商品分类信息
	 */
	public List<ProductCategory> finSelectProductCategory(int type) {
		ProductCategoryDao pro = MybatisUtil.getMapper(ProductCategoryDao.class);
		List<ProductCategory> list = pro.selectProductCategory(type);
		MybatisUtil.closeCommit();
		return list;
	}

	@Override
	/**
	 * 查询所有商品信息
	 */
	public List<Product> finSelectProductAll() {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		List<Product> list = pcd.seleteProductAll();
		MybatisUtil.closeCommit();
		return list;
	}

	@Override
	/**
	 * 根据id查找商品信息
	 */
	public Product finBtnProduct(int id) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int count = finProductCount(id);
		Product pro;
		if(count > 0) {
			pro = pcd.btnProduct(id);
		}else {
			pro = null;
		}
		return pro;
	}

	@Override
	/**
	 * 查询分类的type值
	 */
	public int finProductCount(int categoryid) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int productCount = pcd.productCount(categoryid);
		MybatisUtil.closeCommit();
		return productCount;
	}

	@Override
	/**
	 * 查询商品对应的type的商品
	 */
	public List<Product> finPageProduct(int categoryid, Pager page ) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int type = pcd.categoryType(categoryid);
		List<Product> pageProduct = pcd.pageProduct(type, categoryid, page);
		MybatisUtil.closeCommit();
		return pageProduct;
	}

	@Override
	/**
	 * 查询分类的商品总数	
	 */
	public int finPageCount(int categoryid) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		//查询分类的的等级
		int type  = pcd.categoryType(categoryid);
		int pageCount = pcd.pageCount(type, categoryid);
		MybatisUtil.closeCommit();
		return pageCount;
	}

	@Override
	public List<Product> finLikeProduct(String name, Pager page) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		List<Product> likeProduct = pcd.likeProduct(name, page);
		MybatisUtil.closeCommit();
		return likeProduct;
	}

	@Override
	public int finLikeCount(String name) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int likeCount = pcd.likeCount(name);
		MybatisUtil.closeCommit();
		return likeCount;
	}

	@Override
	public List<Product> CollectProduct(List<Collect> list) {
		ProductCategoryDao pcd  = MybatisUtil.getMapper(ProductCategoryDao.class);
		List<Product> list2 = pcd.CollectProduct(list);
		MybatisUtil.closeCommit();
		return list2;
	}

	@Override
	public int finUpdatePro(int stock,int proid) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int updatePro = pcd.updatePro(stock,proid);
		MybatisUtil.closeCommit();
		return updatePro;
	}
	
    @Override
    /**
	 * 获得购物车商品所有信息！
	 * @param productId
	 * @param quantityStr
	 * @param cart
	 * @return
	 * @throws Exception
	 */
    public CartUtil modifyShoppingCart(String productId, String quantityStr, CartUtil cart) throws Exception {
    	Integer quantity = 0;
    	if (!EmptyUtils.isEmpty(quantityStr))
            quantity = Integer.parseInt(quantityStr);
        //便利购物车寻找该商品 修改其数量
        for (Cart item : cart.getList()) {
            if (item.getProduct().getId()==(Integer.parseInt(productId))) {
                if (quantity == 0 || quantity < 0) {
                    cart.getList().remove(item);
                    break;
                } else {
                    item.setStock(quantity);
                }
            }
        }
        //重新计算金额
        calculate(cart);
        return cart;
    }

    /**
     * 核算购物车的金额！
     *
     * @param cart
     * @return
     * @throws Exception
     */
    @Override
    public CartUtil calculate(CartUtil cart) throws Exception {
        double sum = 0.0;
        for (Cart item : cart.getList()) {
            sum = sum + item.getStock() * item.getProduct().getPrice();
        }
        cart.setSum(sum);
        return cart;
    }

	@Override
	public int finProCount() {
		ProductCategoryDao pro = MybatisUtil.getMapper(ProductCategoryDao.class);
		int proCount = pro.proCount();
		MybatisUtil.closeCommit();
		return proCount;
	}

	@Override
	public int finCategoryCount() {
		ProductCategoryDao pro = MybatisUtil.getMapper(ProductCategoryDao.class);
		int categoryCount = pro.categoryCount();
		MybatisUtil.closeCommit();
		return categoryCount;
	}

	@Override
	public List<ProductCategory> getProductCategoryAll(Pager page) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		List<ProductCategory> productCategoryAll = pcd.ProductCategoryAll(page);
		MybatisUtil.closeCommit();
		return productCategoryAll;
	}

	@Override
	public int getdParentId(String type,int id) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int parentId = pcd.parentId(type,id);
		MybatisUtil.closeCommit();
		return parentId;
	}

	@Override
	public int findParentId(int parentId) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int parentById = pcd.parentById(parentId);
		MybatisUtil.closeCommit();
		return parentById;
	}

	@Override
	public int deleteEasybuyProductCategoryById(int id) {
		ProductCategoryDao pcd = MybatisUtil.getMapper(ProductCategoryDao.class);
		int row = pcd.delEasybuyProductCategoryById(id);
		MybatisUtil.closeCommit();
		return row;
	}

	
}
